﻿using System;
using System.Collections.Generic;
using Loki.Utils;

namespace Luna.Connectors.ZohoCRM
{
    public sealed class UserContext : LokiObject
    {
        internal UserContext(ZohoCrmClient P_Client, ZohoUser P_User)
        {
            if (P_User == null)
            {
                throw BuildError<ArgumentNullException>(Resources.Errors.ASSERT_USER_REQUIRED);
            }

            Client = P_Client;
            User = P_User;
        }

        private ZohoCrmClient Client { get; set; }

        public ZohoUser User { get; private set; }

        public ZohoInsertResponse InsertRecord<T>(T P_Record) where T : ZohoEntity
        {
            if (P_Record == null)
            {
                throw BuildError<ArgumentNullException>(Resources.Errors.ASSERT_RECORD_REQUIRED);
            }

            P_Record.SetValue(SerializerParams.USER_OWNER_ID, User.ID);
            return Client.InsertRecord(P_Record);
        }

        public ZohoInsertResponse InsertRecords<T>(IEnumerable<T> P_Records) where T : ZohoEntity
        {
            foreach (var L_Record in P_Records)
            {
                L_Record.SetValue(SerializerParams.USER_OWNER_ID, User.ID);
            }

            return Client.InsertRecords(P_Records);
        }

        public bool GetOwnership<T>(T P_Record) where T : ZohoEntity
        {
            P_Record.SetValue(SerializerParams.USER_OWNER_ID, User.ID);
            return Client.UpdateRecord(P_Record);
        }
    }
}